package com.example.firstapp.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.location.Location;

import com.example.firstapp.entity.Domain.Mode;
import com.example.firstapp.entity.Restaurant;

public class RecommenderEngine {

	private double spatialWeight = 1.0;
	private double socialWeight = 1.0;
	private double preferenceWeight = 1.0;

	private final SpatialContext spa;
	private final SocialContext soc;
	private final PreferenceContext pref;
	private Mode mode;
	private Location location;

	public RecommenderEngine(SpatialContext spa, SocialContext soc,
			PreferenceContext pref, Mode mode, Location location) {
		this.spa = spa;
		this.soc = soc;
		this.pref = pref;
		this.mode = mode;
		this.location = location;
	}

	public List<Restaurant> process(List<Restaurant> restaurants) {
		initWeight();
		List<Restaurant> result = new ArrayList<Restaurant>(restaurants.size());
		for (Restaurant restaurant : restaurants) {
			double value = spa.evaluate(restaurant) * spatialWeight
					+ soc.evaluate(restaurant) * socialWeight
					+ pref.evaluate(restaurant) * preferenceWeight;
			// RestaurantResult res = new RestaurantResult(restaurant.getName(),
			// restaurant.getAverageRating(), restaurant.getAddress(),
			// distanceTo(restaurant));
			restaurant.setDistance(distanceTo(restaurant));
			restaurant.setSystemScore(value);
			result.add(restaurant);
		}
		Collections.sort(result, new RestaurantComparator());
		Collections.reverse(result);
		return result;
	}

	private String distanceTo(Restaurant restaurant) {
		int distance = spa.distanceTo(restaurant);
		return distance + " m";
	}

	/**
	 * init weight for each contextual factor
	 */
	private void initWeight() {
		switch (mode) {
		case SPATIAL:
			spatialWeight = 3.0;
			socialWeight = 1.0;
			preferenceWeight = 1.0;
			break;
		case SOCIAL:
			spatialWeight = 1.0;
			socialWeight = 3.0;
			preferenceWeight = 1.0;
			break;
		case PREFERENCE:
			spatialWeight = 1.0;
			socialWeight = 1.0;
			preferenceWeight = 3.0;
			break;
		default:
			break;
		}

	}
}
